package com.sotnyk.sspeed.views;


import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.sotnyk.sspeed.R;
import com.sotnyk.sspeed.utils.ExceedingState;
import com.sotnyk.sspeed.utils.SsAppState;

public class SpeedTextView extends TextView {

    private ExceedingState mExceedingState = ExceedingState.NOT_ACTUAL;
    private SsAppState mState;

    @SuppressWarnings("unused")
    public SpeedTextView(Context context) {
        super(context);
        if (!isInEditMode())
            mState = SsAppState.get(context);
    }

    @SuppressWarnings("unused")
    public SpeedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode())
            mState = SsAppState.get(context);
    }

    @SuppressWarnings("unused")
    public SpeedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode())
            mState = SsAppState.get(context);
    }

    @SuppressWarnings("unused")
    public ExceedingState getExceedingState() {
        return mExceedingState;
    }

    public void setExceedingState(ExceedingState fixState) {
        mExceedingState = fixState;
        updateStyle();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        updateStyle();
    }

    private void updateStyle() {
        if (mState == null) return;
        if (mState.getDarkThemeUsed()) {
            switch (mExceedingState) {
                case NOT_AVAILABLE:
                    setTextAppearance(R.style.SpeedText_DarkNotAvailable);
                    break;
                case NOT_ACTUAL:
                    setTextAppearance(R.style.SpeedText_DarkNotActual);
                    break;
                case IN_LIMITS:
                    setTextAppearance(R.style.SpeedText_DarkInLimits);
                    break;
                case NEAR_LIMIT:
                    setTextAppearance(R.style.SpeedText_DarkNearLimit);
                    startAlarmAnimation();
                    break;
                case LIMIT_EXCEEDED:
                    setTextAppearance(R.style.SpeedText_DarkLimitExceeded);
                    startAlarmAnimation();
                    break;
            }
        } else {
            switch (mExceedingState) {
                case NOT_AVAILABLE:
                    setTextAppearance(R.style.SpeedText_LightNotAvailable);
                    break;
                case NOT_ACTUAL:
                    setTextAppearance(R.style.SpeedText_LightNotActual);
                    break;
                case IN_LIMITS:
                    setTextAppearance(R.style.SpeedText_LightInLimits);
                    break;
                case NEAR_LIMIT:
                    setTextAppearance(R.style.SpeedText_LightNearLimit);
                    startAlarmAnimation();
                    break;
                case LIMIT_EXCEEDED:
                    setTextAppearance(R.style.SpeedText_LightLimitExceeded);
                    startAlarmAnimation();
                    break;
            }
        }
    }

    private void setTextAppearance(int styleId) {
        if (!isInEditMode())
            setTextAppearance(getContext().getApplicationContext(), styleId);
    }

    private void startAlarmAnimation() {
        startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out));
    }
}
